package DMSXL._2024.TX;

import java.util.Arrays;

public class eraseOverlapIntervals {
    public static void main(String[] args) {
        int[][] intervals = {{1,2},{2,3},{3,4},{1,3}};
        System.out.println(new eraseOverlapIntervals().eraseOverlapIntervals(intervals));
    }
    public int eraseOverlapIntervals(int[][] intervals) {
        //把intervals按第一位从大到小排序
        Arrays.sort(intervals,(a,b) -> Integer.compare(a[0],b[0]));
        //记录一下区域
        int left = intervals[0][0];
        int right = intervals[0][1];
        int count = 0;
        for (int i = 1; i < intervals.length; i++) {
            //有重叠
            if(intervals[i][0] < right){
                count++;
                left = intervals[i][0];
                right = Math.min(right,intervals[i][1]);
            }else {
                left = intervals[i][0];
                right = intervals[i][1];
            }
        }
        return count;
    }
}
